﻿// Copyright (C) 2012 Kim Jensen, TaskJunction.com
// Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TaskJunctionLib.Model;

namespace TaskJunctionLib.Mapping.JSON
{
    public class TaskMapper : JSONMapper<Task>
    {
        public override void deserializeFromJSON(System.Json.JsonValue jsonObject, Task obj)
        {
            double estimate, todo;
            int owner, createdby, updatedby, priority, iteration;
            obj.Id = jsonObject["id"].ReadAs<Int32>();
            obj.Description = jsonObject.ValueOrDefault("description").ReadAs<string>("");
            obj.Estimate = (jsonObject.ValueOrDefault("estimate").TryReadAs<Double>(out estimate)) ? (double?)estimate : null;
            obj.Todo = (jsonObject.ValueOrDefault("todo").TryReadAs<Double>(out todo)) ? (double?)todo : null;
            obj.TaskPriorityId = (jsonObject.ValueOrDefault("task_priority_id").TryReadAs<Int32>(out priority)) ? (int?)priority : null;
            obj.TaskStatusId = jsonObject["task_status_id"].ReadAs<int>();
            obj.Title = jsonObject["title"].ReadAs<string>();
            obj.ProjectId = jsonObject["project_id"].ReadAs<int>();
            obj.TaskTypeId = jsonObject["task_type_id"].ReadAs<int>();
            obj.OwnerUserId = (jsonObject.ValueOrDefault("owner_user_id").TryReadAs<Int32>(out owner)) ? (int?)owner : null;
            obj.CreatedAt = jsonObject.ValueOrDefault("created_at").ReadAs<DateTime>();
            obj.CreatedByUserId = (jsonObject.ValueOrDefault("created_user_id").TryReadAs<Int32>(out createdby)) ? (int?)createdby : null;
            obj.UpdatedAt = jsonObject.ValueOrDefault("updated_at").ReadAs<DateTime>();
            obj.UpdatedByUserId = (jsonObject.ValueOrDefault("edited_user_id").TryReadAs<Int32>(out updatedby)) ? (int?)updatedby : null;
            obj.IterationId = (jsonObject.ValueOrDefault("iteration_id").TryReadAs<Int32>(out iteration)) ? (int?)iteration : null;
        }

        public override void serializeToJSON(System.Json.JsonValue jsonObject, Task obj)
        {
            jsonObject["id"] = obj.Id;
            jsonObject["description"] = obj.Description;
            jsonObject["estimate"] = obj.Estimate;
            jsonObject["todo"] = obj.Todo;
            jsonObject["task_priority_id"] = obj.TaskPriorityId;
            jsonObject["task_status_id"] = obj.TaskStatusId;
            jsonObject["title"] = obj.Title;
            jsonObject["project_id"] = obj.ProjectId;
            jsonObject["task_type_id"] = obj.TaskTypeId;
            jsonObject["owner_user_id"] = obj.OwnerUserId;
            jsonObject["created_at"] = obj.CreatedAt;
            jsonObject["created_user_id"] = obj.CreatedByUserId;
            jsonObject["edited_user_id"] = obj.UpdatedByUserId;
            jsonObject["updated_at"] = obj.UpdatedAt;
            jsonObject["iteration_id"] = obj.IterationId;
        }

        protected override string JSONName()
        {
            return "task";
        }
    }
}
